package com.gduf.bbl.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gduf.bbl.common.utils.MD5Utils;
import com.gduf.bbl.common.utils.PageUtils;
import com.gduf.bbl.common.utils.Query;
import com.gduf.bbl.common.utils.R;
import com.gduf.bbl.dao.AddressDao;
import com.gduf.bbl.dao.AreaDao;
import com.gduf.bbl.entity.UserUpdateVo;
import com.gduf.bbl.entity.UserVo;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.apache.catalina.User;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;


import com.gduf.bbl.dao.UserDao;
import com.gduf.bbl.entity.UserEntity;
import com.gduf.bbl.service.UserService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {

    @Resource
    private AreaDao areaDao;

    @Resource
    private AddressDao addressDao;


    @Resource
    private UserDao userDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UserEntity> page = this.page(
                new Query<UserEntity>().getPage(params),
                new QueryWrapper<UserEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public R login(UserVo userVo) {
        final UserEntity userEntity = new UserEntity();
        userEntity.setUsername(userVo.getNickName());
        userEntity.setDtype(4);
        userEntity.setIsDelete(2);
        userEntity.setPwd(MD5Utils.md5("12345678"));
        userEntity.setUserState("1");
        userEntity.setAreaId("1");
        userEntity.setDeadline(new Date());
        userEntity.setUserImg(userVo.getAvatarUrl());

        //查询是否有这个账户
        final QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", userEntity.getUsername());
        List<UserEntity> userEntities = this.baseMapper.selectList(queryWrapper);
        if(userEntities.size() == 0){
            //没有就新建账户
            int i = this.baseMapper.insert(userEntity);
        }
        List<UserEntity> userEntities1 = this.baseMapper.selectList(queryWrapper);
        //获取token

        JwtBuilder builder = Jwts.builder();

        String token = builder.setSubject(userEntity.getUsername()) //token携带的数据
                .setIssuedAt(new Date())//设置token的生成时间
                .setId(userEntities1.get(0).getId()+"")//设置用户id
                .setExpiration(new Date(System.currentTimeMillis()+1000*60*60*24*3))//设置三天后过期
                .signWith(SignatureAlgorithm.HS256,"bblXQW")//编码方式，加密的密码
                .compact();
        Map<String,Object> map = new HashMap<>();
        map.put("token",token);
        map.put("userinfo",userEntities1.get(0));

        return R.ok().setData(map);

    }

    @Override
    public R getYjByUserId(Integer id) {
        return null;
    }

    @Override
    @Transactional
    public R updateUser(UserUpdateVo updateVo) {
        //修改用户信息表
        int i1 = userDao.updateUser(updateVo.getUsername(),updateVo.getPhone(),updateVo.getUserImg(),updateVo.getUserId());

        //修改address
        int i2 = addressDao.updateAddress(updateVo.getAddress(),updateVo.getUserId());

        //修改area
        int i3 = areaDao.updateArea(updateVo.getSchool(),updateVo.getAreaId());

        if(i1 > 0 && i2 > 0 && i3 > 0){
            return R.ok().setData(updateVo.getUserImg());
        }else{
            return R.error();
        }

    }

}